package com.it.controller;

import com.it.pojo.Menu;
import com.it.service.MenuRoleService;
import com.it.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/19 16:15
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuRoleService menuRoleService;

    /*已有权限查询*/
    @GetMapping("/findRid/{id}")
    @ResponseBody
    public List<Menu> findRid(@PathVariable("id") String roleId) {
        //System.out.println("角色id:"+roleId);
        List<Menu> list = menuService.findByRoleId(roleId);
        return list;
    }

    /*未有权限查询*/
    @GetMapping("/findNoRid/{id}")
    @ResponseBody
    public List<Menu> findNoRid(@PathVariable("id") String roleId) {
        List<Menu> list = menuService.findByNoRoleId(roleId);
        return list;
    }

    /*移除权限*/
    @PostMapping("/min/{leftId}/{roleId}")
    @ResponseBody
    public Integer minMenuId(@PathVariable("leftId") String menuId, @PathVariable("roleId") String roleId) {
        //System.out.println(menuId+"\t"+roleId);
        int i = menuRoleService.updateByMidAndRid(menuId, roleId);
        return i;
    }

    /*添加权限*/
    @PostMapping("/add/{rightId}/{roleId}")
    @ResponseBody
    public Integer maxMenuId(@PathVariable("rightId") String menuId, @PathVariable("roleId") String roleId) {
        //System.out.println(menuId+"\t"+roleId);
        int i = menuRoleService.updateAddMidAndRid(menuId, roleId);
        return i;
    }

    /*查询所有一级菜单*/
    @GetMapping("/list")
    public ModelAndView findAll() {
        ModelAndView modelAndView = new ModelAndView();
        List<Menu> menuList = menuService.findAll();
        modelAndView.addObject("menuList", menuList);
        modelAndView.setViewName("pages/user/menuList");
        return modelAndView;
    }

    /*查询所有二级菜单*/
    @GetMapping("/menuList2/{levelId1}")
    public String menuList2(@PathVariable("levelId1") String menuId, HttpSession session) {
        List<Menu> list = menuService.findAllMenuList2(menuId);
        session.setAttribute("menuId", menuId);
        session.setAttribute("menuList2", list);
        return "pages/user/menuList2";
    }

    /*删除一级菜单*/
    @GetMapping("/deleteByMenuId/{menuId}")
    public String deleteByMenuId(@PathVariable("menuId") String menuId) {
        menuService.deleteByMenuId(menuId);
        return "redirect:/menu/list";
    }

    /*回显一级菜单*/
    @GetMapping("/findById/{menuId}")
    @ResponseBody
    public Menu findById(@PathVariable("menuId") String menuId) {
        Menu menu = menuService.findById(menuId);
        return menu;
    }

    /*添加或修改一级菜单*/
    @PostMapping("/add")
    public String add(Menu menu) {
        menuService.addMenuLevelOne(menu);
        return "redirect:/menu/list";
    }

    /*删除二级菜单*/
    @GetMapping("/deleteByMenuId2/{mid2}/{mid1}")
    public String deleteByMenuId2(@PathVariable("mid2") String menuId, @PathVariable("mid1") String mid) {
        System.out.println(mid);
        menuService.deleteByMenuId(menuId);
        return "redirect:/menu/menuList2/"+mid;
    }

    /*添加或修改二级菜单*/
    @PostMapping("/addTwo")
    public String addTwo(Menu menu){
        menuService.addMenuLevelOne(menu);
        return "redirect:/menu/menuList2/"+menu.getMenuParentid();
    }


}
